package testutil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ejb.EJB;
import javax.ejb.Stateless;

import dao.TestQuestionDAORemote;
import dto.QuestionDTO;
import dto.TestQuestionDTO;

/**
 * Session Bean implementation class ScoreComputationBean
 */
@Stateless
public class ScoreComputationBean implements ScoreComputationBeanRemote {

	@EJB
	TestQuestionDAORemote testQuestionDAO;

	public ScoreComputationBean() {
	}

	public Float computeTotalScore(int interviewId) throws Exception {
		System.out.println("aaaaaaaaaaaaaaaaaaaa"+interviewId);
		try {
			Float totalScore = (float) 0;
			List<TestQuestionDTO> tqList = testQuestionDAO
			.getInterviewQuestions(interviewId);
			
			// iterate over the questions in interview
			for (TestQuestionDTO tqDTO : tqList) {
				QuestionDTO qDTO = tqDTO.getQuestion();
				
				// add to total
				totalScore += getPointsForQuestion(String.valueOf(
						qDTO.getCorrectAnswers()).trim(), String.valueOf(
								tqDTO.getCandidateAnswer()).trim());
			}
			return totalScore;
		} catch (Exception e) {
			throw new Exception(
					"computeCorrectAnswersPerCategories - ScoreComputationBean - Exception",
					e);
		}
	}

	public Map<Integer, List<Float>> computeCorrectAnswersPerCategories(int 
			interviewId) throws Exception {
		try {
			Map<Integer, List<Float>> categoriesScore = new HashMap<Integer, List<Float>>();
			List<TestQuestionDTO> tqList = testQuestionDAO
			.getInterviewQuestions(interviewId);
			
			// iterate over the questions in interview
			for (TestQuestionDTO tqDTO : tqList) {
				QuestionDTO qDTO = tqDTO.getQuestion();
				Integer catId = qDTO.getCategory().getId();
				List<Float> tempList = new ArrayList<Float>();
				
				// variable for keeping previous value
				float prevNrAnswers = 0;
				
				// if category has already been added to the map
				if (categoriesScore.containsKey(catId)) {
					
					// increment number of questions in category
					tempList.add(categoriesScore.get(catId).get(0) + 1);
					prevNrAnswers = categoriesScore.get(catId).get(1);
				} else {
					
					// set number of questions in new category to 1
					tempList.add((float) 1);
				}
				
				// add points to list and update map
				tempList.add(getPointsForQuestion(String.valueOf(
						qDTO.getCorrectAnswers()).trim(), String.valueOf(
								tqDTO.getCandidateAnswer()).trim())
								+ prevNrAnswers);
				categoriesScore.put(catId, tempList);
			}
			return categoriesScore;
		} catch (Exception e) {
			throw new Exception(
					"computeCorrectAnswersPerCategories - ScoreComputationBean - Exception",
					e);
		}
	}

	public Map<Integer, Float> getQuestionsAndAnswers(int interviewId) throws Exception {
		try {
			Map<Integer, Float> pointsQuestions = new HashMap<Integer, Float>();
			Float score = (float) 0;
			List<TestQuestionDTO> tqList = testQuestionDAO
			.getInterviewQuestions(interviewId);
			
			// iterate over the questions in interview
			for (TestQuestionDTO tqDTO : tqList) {
				QuestionDTO qDTO = tqDTO.getQuestion();
				
				// add to total
				score = getPointsForQuestion(String.valueOf(
						qDTO.getCorrectAnswers()).trim(), String.valueOf(
								tqDTO.getCandidateAnswer()).trim());
				pointsQuestions.put(qDTO.getId(), score);
			}
			return pointsQuestions;
		} catch (Exception e) {
			throw new Exception(
					"getQuestionsAndAnswers - ScoreComputationBean - Exception",
					e);
		}
	}
	
	public float getPointsForQuestion(String correctAnswers,
			String candidateAnswers) {
		
		// if number of correct answers == 1
		if (correctAnswers.length() == 1) {
			// check candidate's answer length & content
			if ((candidateAnswers.length() == 1)
					&& (candidateAnswers.charAt(0) == correctAnswers.charAt(0))) {
				return 1;
			}
		} 
		// if number of correct answers == 2
		else if (correctAnswers.length() == 2) {
			// if candidate's answers length == 2
			if (candidateAnswers.length() == 2) {
				// check if candidate's answers match
				if ((correctAnswers.charAt(0) == candidateAnswers.charAt(0) || correctAnswers
						.charAt(0) == candidateAnswers.charAt(1))
						&& (correctAnswers.charAt(1) == candidateAnswers
								.charAt(0) || correctAnswers.charAt(1) == candidateAnswers
								.charAt(1))) {
					return 1;
				}
			}
			// if candidate's answer length == 1
			else if (candidateAnswers.length() == 1) {
				// half point if answer matches one correct answer
				if (candidateAnswers.charAt(0) == correctAnswers.charAt(0)
						|| candidateAnswers.charAt(0) == correctAnswers
						.charAt(1)) {
					return (float) (0.5);
				}
			}
		}
		return 0;
	}
	
	@Override
	public boolean isInterviewStarted(int interviewId) throws Exception {
		try {
			boolean ok=true;
			List<TestQuestionDTO> tqList = testQuestionDAO
			.getInterviewQuestions(interviewId);
			
			// iterate over the questions in interview
			if(tqList.size()!=0)
			for (TestQuestionDTO tqDTO : tqList) {
				if(tqDTO.getCandidateAnswer()==-1)
				{
					ok=false;
					break;
				}
			}
			return ok;
		} catch (Exception e) {
			throw new Exception(
					"computeCorrectAnswersPerCategories - ScoreComputationBean - Exception",
					e);
		}
	}

}
